package poc.utils;

import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.mce.AlternateContent;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.toc.TocHelper;
import org.docx4j.vml.CTGroup;
import org.docx4j.vml.CTRect;
import org.docx4j.vml.CTTextbox;
import org.docx4j.vml.VmlShapeElements;
import org.docx4j.wml.*;

import javax.xml.bind.JAXBElement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by zhangleilei on 2018/10/11.
 */
public class ParagraphUtil {
    public static List<Object> getFootNotesParagraphs(MainDocumentPart part) {
        ArrayList result = new ArrayList();
        if (part.hasFootnotesPart()) {
            CTFootnotes footnotesWrapped = part.getFootnotesPart().getJaxbElement();
            for (CTFtnEdn ctFtnEdn:footnotesWrapped.getFootnote()){
                for (Object obj:ctFtnEdn.getContent()){
                    result.addAll(TocHelper.getAllElementsFromObject(obj, P.class));
                }
            }
        }
        return result;
    }

    public static List<P> getAllPInPicture(Object object) {
        ArrayList result = new ArrayList();
        List<Object> picts= getAllElementsFromObject(object, Pict.class);
//        System.out.println("pict size is"+ picts.size());
        for (Object pict:picts){
            result.addAll(getAllElementsFromObject(pict,P.class));
        }
        return result;
    }

    public static List<P> getAllWTParagraphInPicture(Object object){
        ArrayList result = new ArrayList();
        List<Object> picts= getAllElementsFromObject(object, Pict.class);
//        System.out.println("pict size is"+ picts.size());
        for (Object pict:picts){
            result.addAll(getAllElementsFromObject(pict,P.class));
        }
        return result;
    }

    private static List<Object> getAllElementsFromObject(Object obj, Class<?> toSearch) {
        ArrayList result = new ArrayList();
        if(obj instanceof JAXBElement) {
            obj = ((JAXBElement)obj).getValue();
        }

        if(obj instanceof SdtElement) {
            obj = ((SdtElement)obj).getSdtContent();
        }

        if(obj.getClass().equals(toSearch)) {
            result.add(obj);
        } else if(obj instanceof ContentAccessor) {
            List children = ((ContentAccessor)obj).getContent();
            Iterator var4 = children.iterator();

            while(var4.hasNext()) {
                Object child = var4.next();
                result.addAll(getAllElementsFromObject(child, toSearch));
            }
        }else if(obj instanceof Pict) {
            List children = ((Pict)obj).getAnyAndAny();
            Iterator var4 = children.iterator();

            while(var4.hasNext()) {
                Object child = var4.next();
                result.addAll(getAllElementsFromObject(child, toSearch));
            }
        }else if(obj instanceof VmlShapeElements) {
            List children = ((VmlShapeElements)obj).getEGShapeElements();
            Iterator var4 = children.iterator();

            while(var4.hasNext()) {
                Object child = var4.next();
                result.addAll(getAllElementsFromObject(child, toSearch));
            }
        }else if(obj instanceof CTTextbox) {
            List children = ((CTTextbox)obj).getTxbxContent().getContent();
            Iterator var4 = children.iterator();

            while(var4.hasNext()) {
                Object child = var4.next();
                result.addAll(getAllElementsFromObject(child, toSearch));
            }
        }else if(obj instanceof AlternateContent) {
            List children = ((AlternateContent)obj).getFallback().getAny();
            Iterator var4 = children.iterator();

            while(var4.hasNext()) {
                Object child = var4.next();
                result.addAll(getAllElementsFromObject(child, toSearch));
            }
        }

        return result;
    }
}
